package com.herohead.json;

import java.util.HashMap;
import java.util.Map;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import com.herohead.Email;

public class JsonEmail {


	public static String createJsonLocal() {

		JSONObject jsobj1 = new JSONObject();
		jsobj1.putAll(getEmailPair());
		return jsobj1.toJSONString();
	}
	public static JSONObject getJsonObject(String json){
		JSONParser parser = new JSONParser();
		Object obj1 = null;
		try {
			obj1 = parser.parse(json);
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		emailJson =(JSONObject) obj1;

		return emailJson;
	}

	String emailTo;
	String emailFrom;
	String emailSubject;
	String emailType= "0";
	/* 0 = normal text
	 * 1 = html
	 * 2 = html with text
	 * 3 = attachment
	 */


	protected String emailServer;
	String emailBody;
	String emailBodyText;
	static JSONObject emailJson = new JSONObject();

	private static Map<String, String> emailPair = new HashMap<String, String>();
	public static Map<String, String> getEmailPair() {
		return emailPair;
	}
	public static void setEmailPair(Map<String, String> emailPair) {
		JsonEmail.emailPair = emailPair;
	}
	String jsonString;
	String toField = "To";
	String fromField = "From";
	String emailTypeField = "emailType";

	protected String serverField = "Server";


	String bodyField = "body";

	String subjectField = "subject";
	String bodyTextField = "bodyText";

	public JsonEmail(String jsonString) {
		this.jsonString = jsonString;
		getJsonObject(this.jsonString);
	}

	public JsonEmail(String emailTo,String emailFrom,String emailSubject, String emailBody, String emailType) {
		this(emailTo,emailFrom,emailSubject, "smtp.mt.com", emailBody,emailType);
	}

	public JsonEmail(String emailTo,String emailFrom,String emailSubject, String emailServer, String emailBody,String emailType) {
		this(emailTo,emailFrom,emailSubject, "smtp.mt.com", emailBody,"",emailType);
	}

	public JsonEmail(String emailTo,String emailFrom,String emailSubject, String emailServer, String emailBody,String emailBodyText,String emailType) {
		this.emailType = emailType;
		this.emailTo = emailTo;
		this.emailBody = emailBody;
		this.emailFrom = emailFrom;
		this.emailServer = emailServer;
		this.emailSubject = emailSubject;
		this.emailBodyText = emailBodyText;

		getEmailPair().put(emailTypeField, this.emailType);
		getEmailPair().put(toField, this.emailTo);
		getEmailPair().put(fromField, this.emailFrom);
		getEmailPair().put(subjectField, this.emailSubject);
		getEmailPair().put(serverField, this.emailServer);
		getEmailPair().put(bodyField, this.emailBody);
		getEmailPair().put(bodyTextField, this.emailBodyText);
	}

	public String getBody() {
		return getValue(bodyField);
	}
	public String getBodyText() {
		return getValue(bodyTextField);
	}
	public String getEmailType() {
		System.out.println("in getEmailType " + getValue(emailTypeField));
		return getValue(emailTypeField);
	}

	public String getFrom() {
		return getValue(fromField);
	}

	public JSONObject getJsonObject() {
		JSONObject jsobj1 = new JSONObject();
		jsobj1.putAll(getEmailPair());
		return jsobj1;
	}

	public String getJsonString() {
		return createJsonLocal();
	}
	public String getServer() {
		return getValue(serverField);
	}
	public String getSubject() {
		return getValue(subjectField);
	}
	public String getTo() {
		return getValue(toField);
	}

	public String getValue(Object value) {
		return (String) emailJson.get(value);
	}
	public void sendAttEmail() {
		Email em = new Email(this.getServer());
		em.addToAddress(this.getTo());
		em.addFromAddress(this.getFrom());
		em.setHost(this.getServer());
		em.sendMailAttv2(this.getSubject(), this.getBody(),"here is the contents");
	}

	public void sendEmail() {

		String etype = this.getEmailType();
		System.out.println("etype :" + etype);

		if (etype.contentEquals("0")) {
			this.sendTextEmail();
		} else if (etype.contentEquals("1")) {
			System.out.println("Found type 1");
			this.sendHtmlEmail();
		} else if (etype.contentEquals("2")) {
			this.sendHtmlTextEmail();
		} else if (etype.contentEquals("3")) {
			this.sendAttEmail();
		}

	}

	public void sendHtmlEmail() {
		Email em = new Email(this.getServer());
		em.addToAddress(this.getTo());
		em.addFromAddress(this.getFrom());
		em.setHost(this.getServer());
		em.sendHTMLMail(this.getSubject(), this.getBody());
	}


	public void sendHtmlTextEmail() {
		System.out.println("In sendHTMLTextHtml");
		System.out.println(this.getBodyText());
		Email em = new Email(this.getServer());
		em.addToAddress(this.getTo());
		em.addFromAddress(this.getFrom());
		em.setHost(this.getServer());
		//em.sendHTMLTEXTMail(this.getSubject(), this.getBody());
		em.sendHTMLMailWithText(this.getSubject(), this.getBody(), this.getBodyText());
	}

	public void sendTextEmail() {
		Email em = new Email(this.getServer());
		em.addToAddress(this.getTo());
		em.addFromAddress(this.getFrom());
		em.setHost(this.getServer());
		em.sendMail(this.getSubject(), this.getBody());
	}



	public void setServer(String server) {
		this.emailServer = server;
		getEmailPair().put(serverField, this.emailServer);
		getJsonObject(createJsonLocal());
	}

}

